package chap04.Queue;

/**
 * Created by root on 2017/7/13.
 */
class Queue{
    private int maxSize;
    private long[] queArray;
    private int front;
    private int rear;

    public Queue(int s){
        maxSize = s+1;
        queArray = new long[maxSize];
        front = 0;
        rear = -1;
    }

    public void insert(long j){
        if (rear==maxSize-1) {
            rear = -1;
        }
        queArray[++rear] = j;
    }

    public long remove() {
        long temp = queArray[front++];
        if (front == maxSize) {
            front = 0;
        }
        return temp;
    }

    public long peek(){
        return queArray[front];
    }

    public boolean isEmpty(){
        return (rear+1==front || (front+maxSize-1==rear));
    }

    public boolean isFull(){
        return (rear+2==front || (front+maxSize-2==rear));
    }

    public int size(){
        if (rear >= front) {
            return rear - front +1;
        } else {
            return (maxSize - front) + (rear + 1);
        }
    }
}
public class QueueNoNItemsApp {

    public static void main(String[] args){
        int maxSize = 5;
        Queue queue = new Queue(maxSize);
        queue.insert(1);
        queue.insert(2);
        queue.insert(3);
        queue.insert(4);
        queue.insert(5);
    }

}
